package cn.yuanqiao.archive.appManager.service.impl;


import cn.yuanqiao.archive.appManager.domain.YqSy;
import cn.yuanqiao.archive.appManager.mapper.YqSyMapper;
import cn.yuanqiao.archive.appManager.service.IYqSyService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 水印Service业务层处理
 *
 * @author yuanqiao
 * @date 2023-06-16
 */
@Service
public class YqSyServiceImpl implements IYqSyService {
    @Autowired
    private YqSyMapper yqSyMapper;

    /**
     * 查询水印
     *
     * @param ID 水印主键
     * @return 水印
     */
    @Override
    public YqSy selectYqSyByID(Long ID) {
        YqSy yqSy = new YqSy();
        yqSy.setId(ID);
        return yqSyMapper.selectYqSyByID(yqSy);
    }

    /**
     * 查询水印（根据水印代码）
     *
     * @param yqSy 水印
     * @return 水印
     */
    @Override
    public YqSy selectYqSyBySydm(YqSy yqSy) {
        YqSy sy = new YqSy();
        if (null!=yqSyMapper.selectYqSyBySydm(yqSy)){
            sy = yqSyMapper.selectYqSyBySydm(yqSy);
            if (sy.getSetup() == 1) {
                LoginUser user = SecurityUtils.getLoginUser();
                String userName = user.getUsername();
                String userPhone = user.getUser().getPhonenumber();
                Date nowDate = DateUtils.getNowDate();
                SimpleDateFormat rq= new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sj= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                switch (sy.getSynr()) {
                    case "0":
                        sy.setSynr(userName);
                        break;
                    case "1":
                        sy.setSynr(sj.format(nowDate));
                        break;
                    case "2":
                        sy.setSynr(userName + " " + sj.format(nowDate));
                        break;
                    case "3":
                        if (StringUtils.isEmpty(userPhone)||userPhone.length()<4){
                            sy.setSynr(userName + " ****");
                        }else {
                            sy.setSynr(userName + " " + userPhone.substring(userPhone.length() - 4));
                        }
                        break;
                }
            }
        }else {
            sy = new YqSy();
            sy.setSynr("");
        }
        return sy;
    }

    /**
     * 查询水印列表
     *
     * @param yqSy 水印
     * @return 水印
     */
    @Override
    public List<YqSy> selectYqSyList(YqSy yqSy) {
        return yqSyMapper.selectYqSyList(yqSy);
    }

    /**
     * 新增水印
     *
     * @param yqSy 水印
     * @return 结果
     */
    @Override
    public String insertYqSy(YqSy yqSy) {
        StringBuffer successMsg = new StringBuffer();
        LoginUser user = SecurityUtils.getLoginUser();

//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }

        yqSy.setCreatorId(user.getUserId());
        yqSy.setCreate_time(DateUtils.getNowDate());
        if (yqSy.getSetup() != null && yqSy.getSetup() == 0 && !StringUtils.isNotEmpty(yqSy.getSynr())) {
            successMsg.append("水印内容不能为空！");
        } else {
            if (yqSyMapper.insertYqSy(yqSy) > 0) {
                successMsg.append("添加成功！");
            }
        }
        return successMsg.toString();
    }

    /**
     * 修改水印
     *
     * @param yqSy 水印
     * @return 结果
     */
    @Override
    public String updateYqSy(YqSy yqSy) {
        StringBuffer successMsg = new StringBuffer();
        LoginUser user = SecurityUtils.getLoginUser();

//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }

        yqSy.setLastupdater(user.getUserId());
        yqSy.setLastupdateTime(DateUtils.getNowDate());
        if (yqSy.getSetup() != null && yqSy.getSetup() == 0 && !StringUtils.isNotEmpty(yqSy.getSynr())) {
            successMsg.append("水印内容不能为空！");
        } else {
            if (yqSyMapper.updateYqSy(yqSy) > 0) {
                successMsg.append("修改成功！");
            }
        }
        return successMsg.toString();
    }

    /**
     * 修改水印启用状态
     *
     * @param yqSy 水印
     * @return 结果
     */
    @Override
    public int updateYqSyStatus(YqSy yqSy) {
        return yqSyMapper.updateYqSyStatus(yqSy);
    }

    /**
     * 批量删除水印
     *
     * @param IDs 需要删除的水印主键
     * @return 结果
     */
    @Override
    public int deleteYqSyByIDs(Long[] IDs) {
        YqSy yqSy = new YqSy();
        return yqSyMapper.deleteYqSyByIDs(yqSy.getTableName(), IDs);
    }

    /**
     * 删除水印信息
     *
     * @param ID 水印主键
     * @return 结果
     */
    @Override
    public int deleteYqSyByID(Long ID) {
        return yqSyMapper.deleteYqSyByID(ID);
    }
}
